<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let scoreNumber = 99

        let person = {
            name: '张三',
            sex: '男',
        }
        //为person对象定义一个属性age
        Object.defineProperty(person, 'age', {
            value: 18, //属性值
            enumerable: true, //控制属性是否可以枚举（遍历），默认false
            writable: true, //控制属性是否可以被修改，默认false
            configurable: true, //控制属性是否可被删除，默认false
        })
        console.log(person)
        console.log(Object.keys(person))
        person.age = 19
        console.log(person)
        delete person.age
        console.log(person)

        //为person定义一个score属性，要求读写person.score时，实际上读写的是变量scoreNumber
        Object.defineProperty(person, 'score', {
            //定义get方法(getter)，每次读取score属性时自动调用get方法
            get: function() {
                console.log('有人读取score')
                return scoreNumber
            },
            set: function(val) {
                console.log('有人设置score的值为' + val)
                scoreNumber = val
            }
        })
        console.log(person)
    </script>
</body>
</html>